
var DocsPollerPack = window.DocsPollerPack || {};

DocsPollerPack.ModelInterface = function()
{
        //Nothing here, this is just an interface
}
DocsPollerPack.ModelInterface.prototype.getGDData = function(docKey)
{
        alert("getGDData: Unimplemented, Key : "+docKey);
}
DocsPollerPack.ModelInterface.prototype.getTable = function()
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.getCandidateTotal = function()
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.addsubscriptor = function(subscriptor)
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.getGDKeys = function(docKeys)
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.getData = function(keys) 
{
        alert("Unimplemented Method");
}
DocsPollerPack.ModelInterface.prototype.getRegionKeys = function(key) 
{
        alert("Unimplemented Method");
}
/*data model:
        MAP : KeyMesa -> Total Votos
        LIST : Keys_to_access
        BOOL : RevisarHojasFinales
        MAP : Candidate -> NationwideTotal
        MAP : DocKey - NodeName (Dpto,City,Center,table, etc)
        
*/
DocsPollerPack.Model = function()
{
        this.revisarHojasFinales = false;
        this.mapTotalVotes = {};
        this.keysToAccess = [];
        this.auxKeysToAccess = [];
        this.candidateTotal = [];
        this.names =[];
		this.JSONBuffer = [];
		this.regionNameList = [];
		
        
}
DocsPollerPack.Model.prototype = clone(DocsPollerPack.ModelInterface.prototype);
DocsPollerPack.Model.prototype.constructor = DocsPollerPack.Model;
DocsPollerPack.Model.prototype.addsubscriptor = function(viewSubscriptor)
{
        this.subscriptor = viewSubscriptor;
}
DocsPollerPack.Model.prototype.getTable = function()
{
	var bundle = [];
	bundle.push(this.regionNameList[0]);
	bundle.push(this.names);
	bundle.push(this.candidateTotal);
	return bundle;
}
DocsPollerPack.Model.prototype.getCandidateTotal = function()
{
        return this.mapCandidateTotal
}
DocsPollerPack.Model.prototype.getData = function(key)
{
    //1-get all the info about the root(country)
	this.getGDTable(key);
	//TODO: Make a Node
}
DocsPollerPack.Model.prototype.dataProcess = function() //After Callback, to handle events
{
	for (i in this.auxKeysToAccess){
		this.keysToAccess.push(this.auxKeysToAccess[i]);
	}
	this.auxKeysToAccess = [];
	index = this.keysToAccess.shift();
	console.log(index);
	console.log(this.keysToAccess);
	this.getGDTable(index);
		
}

DocsPollerPack.Model.prototype.nodeProcess = function() //Handling vote tables
{
	if(this.keysToAccess.length>0){
		index = this.keysToAccess.shift();
		this.getGDTable(index);
	}else{
		//Notify view, 
		console.log(this.candidateTotal);
		this.subscriptor.notify();
	}
}

DocsPollerPack.Model.prototype.getGDTable = function(key)
{
	var self = this;
	DocsPollerPack.callback = function(data)
	{
		var response = data.table.rows;
		self.JSONBuffer.push(response);
		//add last node check
		self.level = response[0].c[0].v.toLowerCase();
		self.regionNameList.push(response[0].c[1].v);
		if(self.level != "Mesa".toLowerCase()){
		
			var ndex = 1;
			while (response[ndex].c[0].v == ""){
				ndex++;
			}
			for (var r =ndex;r<response.length;r++){
				self.auxKeysToAccess.push(response[r].c[1].v );
				//add: make nodes and save them
			}
			self.dataProcess();
		}else{
			if (self.names.length == 0){
				//self.keysToAccess.unshift(key);
				console.log(self.keysToAccess);
				//fetch the names
				var ndex = 1;
				while (response[ndex].c[0].v == ""){
					ndex++;
				}
				for (var r=ndex;r<response.length;r++){
					self.names.push(response[r].c[0].v.toLowerCase() );
				}
				for (i in self.names){
					self.candidateTotal[i] = 0;
				}
				console.log(self.names);
				
			}
			//process the node
			var ndex = 1;
			while (response[ndex].c[0].v == ""){
				ndex++;
			}
			for (var i=ndex; i<response.length;i++){
				index = self.names.indexOf(response[i].c[0].v.toLowerCase());
				self.candidateTotal[index] += response[i].c[1].v
			}
			self.nodeProcess()			
		}		
		//Primera mesa encontrada: Obtener names
		//Todas: Tomar los valores de los candidatos		
	}
	var baseURL = "http://spreadsheets.google.com/tq?tqx=responseHandler:DocsPollerPack.callback&key=";
	var capURL = "&pub=1";
	var element = document.createElement("script");
	element.src = baseURL+ key + capURL;
	element.type = "text/javascript";
	document.getElementsByTagName("head")[0].appendChild(element);	
}

DocsPollerPack.Model.prototype.getRegionKeys = function()
{
    var aRegionNames =[];
	var aRegionKeys = [];
	var top = this.JSONBuffer[0];
	var ndex = 1;
	while (top[ndex].c[0].v == ""){
		ndex++;
	}
	for(var i=ndex;i<top.length;i++)
	{
		aRegionNames.push(top[i].c[0].v.toLowerCase());
		aRegionKeys.push(top[i].c[1].v);
	}
	bundle = [];
	bundle.push(aRegionNames);
	bundle.push(aRegionKeys);
	console.log(bundle);
	return bundle;
	
	//Do: JSONBuffer reading and getting those keys
}